import request from '@/utils/request'
import * as mock from './mock/goods'

const isMock = import.meta.env.VITE_API_MOCK === 'true'

/**
 * 获取商品列表
 * @param {Object} params 查询参数
 * @returns {Promise}
 */
export function getGoodsList(params) {
  if (isMock) {
    return Promise.resolve(mock.getGoodsList(params))
  }
  return request({
    url: '/goods/list',
    method: 'get',
    params
  })
}

/**
 * 获取商品详情
 * @param {string|number} id 商品ID
 * @returns {Promise}
 */
export function getGoodsDetail(id) {
  if (isMock) {
    return Promise.resolve(mock.getGoodsDetail(id))
  }
  return request({
    url: `/goods/detail/${id}`,
    method: 'get'
  })
}


/**
 * 获取商品评论
 * @param {string|number} id 商品ID
 * @param {Object} params 查询参数
 * @returns {Promise}
 */
export function getGoodsComments(id, params) {
  if (isMock) {
    return Promise.resolve(mock.getGoodsComments(id, params))
  }
  return request({
    url: `/goods/comments/${id}`,
    method: 'get',
    params
  })
}

/**
 * 获取推荐商品
 * @param {string|number} id 商品ID
 * @param {number} limit 限制数量
 * @returns {Promise}
 */
export function getRecommendedGoods(id, limit = 4) {
  if (isMock) {
    return Promise.resolve(mock.getRecommendedGoods(id, limit))
  }
  return request({
    url: `/goods/recommended/${id}`,
    method: 'get',
    params: { limit }
  })
}

/**
 * 获取商品所有评论
 * @param {string|number} id 商品ID
 * @returns {Promise}
 */
export function getAllGoodsComments(id) {
  if (isMock) {
    return Promise.resolve(mock.getAllGoodsComments(id))
  }
  return request({
    url: `/goods/all-comments/${id}`,
    method: 'get'
  })
}

/**
 * 获取商品在不同币种下的价格
 * @param {Object} params 查询参数
 * @param {string|number} params.specId 规格ID
 * @param {string} params.payIcon 支付币种
 * @returns {Promise}
 */
export function getGoodsPrice(params) {
  if (isMock) {
    // 模拟不同币种的价格转换
    const mockPrice = (parseFloat(params.price || '10.00') * (Math.random() * 0.5 + 0.8)).toFixed(2)
    return Promise.resolve({
      code: 0,
      data: {
        specId: params.specId,
        payIcon: params.payIcon,
        price: mockPrice
      },
      msg: '获取成功'
    })
  }
  return request({
    url: '/goods/price',
    method: 'get',
    params
  })
}

